<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/9/3
 * Time: 上午8:04
 */

namespace app\api\module;

use app\common\model\Club;
use app\common\model\Consortium;
use app\common\model\DeveloperException;
use app\common\model\Settings;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\UserException;

class Union extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
            return true;
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return string
     * @throws DeveloperException
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】联盟商列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_list
             * @apiGroup 联盟商【Union】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             * @apiParam (dataGroup) {String} category_id 类别
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Union",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "category_id":"1",
             *     "page_size":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} union_list 联盟商列表
             *
             * @apiSuccess (union_list-消息列表) {String} title 名称
             * @apiSuccess (union_list-消息列表) {String} tel 电话
             * @apiSuccess (union_list-消息列表) {String} thumb 图片
             * @apiSuccess (union_list-消息列表) {String} id 联盟商id
             * @apiSuccess (union_list-消息列表) {String} address 活动地址
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "union_list": [
             *       {
             *              "id":"1",
             *             "title":"新闻标题",
             *             "tel":"电话",
             *             "thumb":"https://www.baidu.com",
             *             "address":"沂蒙大道"
             *       },
             *       {
             *              "id":"1",
             *             "title":"新闻标题",
             *             "tel":"电话",
             *             "thumb":"https://www.baidu.com",
             *             "address":"沂蒙大道"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "联盟商列表！"
             * }
             *
             */
            case H_UNION_LIST:
                return $this->union_list();

            /**
             * @api {post} / 【1002】联盟商详情
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_info
             * @apiGroup 联盟商【Union】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} union_id 联盟商id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Union",
             *   "interface": "1002",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "union_id": "10",
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {String} title biaoti
             * @apiSuccess (result_data-回执报文信息) {String} uid 关联用户
             * @apiSuccess (result_data-回执报文信息) {String} address 地址
             * @apiSuccess (result_data-回执报文信息) {String} detail 内容详情
             * @apiSuccess (result_data-回执报文信息) {String} thumb 缩略图
             * @apiSuccess (result_data-回执报文信息) {String} tel 电话
             * @apiSuccess (result_data-回执报文信息) {String} category_id 分类
             * @apiSuccess (result_data-回执报文信息) {String} longitude 纬度
             * @apiSuccess (result_data-回执报文信息) {String} latitude 经度
             *

             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *             "title":"新闻标题",
             *             "detail":"xinwenneiroing",
             *             "thumb":"https://www.baidu.com",
             *             "uid":"1",
             *             "address":"dizhi",
             *             "tel":"18854485466",
             *             "longitude":"经度",
             *             "latitude":"纬度",
             *             "category_id":"2"
             *   },
             *   "result_code": "0000",
             *   "result_info": "联盟商详情"
             * }
             *
             */
            case H_UNION_INFO:
                return $this->union_info();
            /**
             * @api {post} / 【1003】卡券列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_card_list
             * @apiGroup 联盟商【Union】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             * @apiParam (dataGroup) {String} category_id 类别(-1:全部)
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Union",
             *   "interface": "1003",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "category_id":"1",
             *     "page_size":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} union_list 联盟商列表
             *
             * @apiSuccess (union_list-消息列表) {String} name 联盟商名称
             * @apiSuccess (union_list-消息列表) {String} thumb 联盟商图标
             * @apiSuccess (union_list-消息列表) {String} discount 折扣
             * @apiSuccess (union_list-消息列表) {String} address 店铺地址
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "union_list": [
             *       {
             *             "name":"咖啡1店",
             *             "discount":"8.5",
             *             "thumb":"https://www.baidu.com",
             *             "address":"沂蒙大道"
             *       },
             *       {
             *             "name":"咖啡1店",
             *             "discount":"8.5",
             *             "thumb":"https://www.baidu.com",
             *             "address":"沂蒙大道"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "优惠券列表！"
             * }
             *
             */
            case H_UNION_CARD_LIST:
                return $this->card_list();
            /**
             * @api {post} / 【1004】卡券使用说明
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_card_explain
             * @apiGroup 联盟商【Union】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Union",
             *   "interface": "1004",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {String} card_explain 使用说明
             *
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "card_explain": "大优惠"
             *   },
             *   "result_code": "0000",
             *   "result_info": "优惠券解释！"
             * }
             *
             */
            case H_UNION_CARD_EXPLAIN:
                return $this->card_explain();
            /**
             * @api {post} / 【1005】线下支付
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_underline_pay
             * @apiGroup 联盟商【Union】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} union_id 联盟商id
             * @apiParam (dataGroup) {String} all_money 总金额
             * @apiParam (dataGroup) {String} money 现金金额
             * @apiParam (dataGroup) {String} coin h币金额
             * @apiParam (dataGroup) {String} pay_type 支付类型（1：微信，2：支付宝）
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Union",
             *   "interface": "1005",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "union_id":"1",
             *     "all_money":"100",
             *     "coin":"100",
             *     "pay_type":"1",
             *     "money":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {string} pay_info 支付信息
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "union_list": "dkjfdkjfkdjfldjljk"
             *   },
             *   "result_code": "0000",
             *   "result_info": "线下支付！"
             * }
             *
             */
            case H_UNION_UNDERLINE_PAY:
                return $this->underline_pay();
            /**
             * @api {post} / 【1006】我是联盟商
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_underline_pay
             * @apiGroup 联盟商【Union】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Union",
             *   "interface": "1006",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {string} pay_info 支付信息
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "money": "dkjfdkjfkdjfldjljk"
             *   },
             *   "result_code": "0000",
             *   "result_info": "我是联盟商！"
             * }
             *
             */
            case H_UNION_IS_UNION:
                return $this->is_union();
            default:
                send_developer_error("模块【活动资源】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    /*
     * 联盟商列表
     * */
    public function union_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $category_id=$this->essentialParam('category_id');
        $user=User::userFromToken($access_token);

        /*城市信息*/
        if($user['bussiness_id']>0){
            $bussiness=Club::bussinessInfoById($user['bussiness_id']);
            if(!$bussiness['city'])
            {
                $city=182;
            }else{
                $city=$bussiness['city'];
            }
        }else{
            $city=182;
        }

        $list=Consortium::consortiumList($category_id,$city,$page,$page_size);
        $this->insertResponse('list',$list);
        return $this->response("联盟商列表！");

    }
    /*
   * 联盟商详情
   * */
    public function union_info()
    {
        $access_token=$this->essentialParam('access_token');
        $user=User::userFromToken($access_token);

        $union_id=$this->essentialParam('union_id');

        $info=Consortium::consortiumInfo($union_id);

        $this->insertResponse('comment',$info['comment']);
        $this->insertResponse('avg',$info['avg']);
        $this->insertResponse('background',$info['background']);
        $this->insertResponse('uid',$info['uid']);
        $this->insertResponse('title',$info['title']);
        $this->insertResponse('detail',$info['detail']);
        $this->insertResponse('thumb',$info['thumb']);
        $this->insertResponse('address',$info['address']);
        $this->insertResponse('tel',$info['tel']);
        $this->insertResponse('category_id',$info['category_id']);
        $this->insertResponse('longitude',$info['longitude']);
        $this->insertResponse('latitude',$info['latitude']);
        return $this->response("联盟商详情");

    }

    /*
        * 优惠券列表
        * */
    public function card_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $category_id=$this->optionalParam('category_id');
        $user=User::userFromToken($access_token);

        /*城市信息*/
        if($user['bussiness_id']>0){
            $bussiness=Club::bussinessInfoById($user['bussiness_id']);
            if(!$bussiness['city'])
            {
                $city=182;
            }else{
                $city=$bussiness['city'];
            }
        }else{
            $city=182;
        }

        /*优惠券分类*/
        if(empty($category_id))
        {
            $category_id="-1";
        }


        $list=Consortium::cardList($page,$page_size,$city,$category_id);
        $this->insertResponse('list',$list);
        return $this->response("优惠券列表！");

    }

    /**
     * 优惠券解释
     * @return array
     * @throws DeveloperException
     */
    public function card_explain()
    {
        $access_token=$this->essentialParam('access_token');

        $info=Settings::getSettings(H_SETTINGS_DISCOUNT_EXPLAIN);
        $this->insertResponse('card_explain',$info);
        return $this->response("优惠券解释！");

    }

    /**
     * 线下支付
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function underline_pay()
    {
        $access_token=$this->essentialParam('access_token');
        $union_id=$this->essentialParam('union_id');
        $all_money=$this->essentialParam('all_money');
        $coin=$this->essentialParam('coin');
        $money=$this->essentialParam('money');
        $pay_type=$this->essentialParam('pay_type');

        $user=User::userFromToken($access_token);
        /*联盟商信息*/
        $info=Consortium::consortiumInfo($union_id);

        /*生成订单*/
        $order=Order::establish($user,$money,$coin,3,$union_id,0,$pay_type,$union_id,$all_money);
        $body=$info['title']."-会链线下消费";
        /*支付类型*/
        if($pay_type==1)
        {
            /*微信支付统一下单*/
            $payInfo=Pay::weixinPay($order['order_sn'],$order['money'],$body);

        }else{
            /*支付宝支付统一下单*/
            $payInfo=Pay::aliPay($order['order_sn'],$order['money'],$body);
        }

        $this->insertResponse('info',$payInfo);

        return $this->response("线下活动");

    }


    /**
     * 我是联盟商
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function is_union()
    {
        $access_token=$this->essentialParam('access_token');

        $user=User::userFromToken($access_token);
        $account=Consortium::accountInfo($user['id']);

        $this->insertResponse('money',$user['money']);
        return $this->response("我是联盟商");

    }


}